package web.action;

import com.opensymphony.xwork2.ActionSupport;
import entity.MyFile;
import org.apache.struts2.ServletActionContext;
import utils.Condition;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class FileListAction extends ActionSupport {

    private HttpServletRequest request = ServletActionContext.getRequest();
    private HttpSession session = request.getSession();
    private List<MyFile> myFileList = (List<MyFile>) session.getAttribute("myFileList");
    private Integer page = (Integer) session.getAttribute("page");

    //按文件类型筛选
    public String screenByFileType() {
        String fileType = request.getParameter("fileType");
        List<String> condition = Condition.getConditionList(fileType);
        myFileList = (List<MyFile>) session.getAttribute("myFileListShadow");

        return returnPage(myFileList.stream().filter(myFile -> condition.contains(myFile.getSuffix())).collect(Collectors.toList()));
    }

    //分页
    public String turnUpPage() {

        System.out.println(page);
        page = page - 1 >= 0 ? page - 1 : 0;

        return returnPage(myFileList);
    }

    public String turnDownPage() {
        System.out.println(page);

        Integer totalPage = (myFileList.size() + 9 - 1) / 9;
        page = page + 1 < totalPage-1 ? page + 1 : totalPage-1;
        System.out.println(page);

        return returnPage(myFileList);
    }

    //上传时间排序
    public String sortByUpLoadDateAsc() {

        myFileList.sort(new Comparator<MyFile>() {
            @Override
            public int compare(MyFile o1, MyFile o2) {

                return o1.getUpload_date().compareTo(o2.getUpload_date());
            }
        });
        return returnPage(myFileList);
    }

    public String sortByUpLoadDateDasc() {

        myFileList.sort(new Comparator<MyFile>() {
            @Override
            public int compare(MyFile o1, MyFile o2) {

                return o2.getUpload_date().compareTo(o1.getUpload_date());
            }
        });
        return returnPage(myFileList);
    }

    //文件名称排序
    public String sortByFileNameAsc() {

        myFileList.sort(new Comparator<MyFile>() {
            @Override
            public int compare(MyFile o1, MyFile o2) {

                return o1.getFilesFileName().compareTo(o2.getFilesFileName());
            }
        });
        return returnPage(myFileList);
    }

    public String sortByFileNameDasc() {

        myFileList.sort(new Comparator<MyFile>() {
            @Override
            public int compare(MyFile o1, MyFile o2) {

                return o2.getFilesFileName().compareTo(o1.getFilesFileName());
            }
        });
        return returnPage(myFileList);
    }

    //文件大小排序
    public String sortByFileSizeAsc() {

        myFileList.sort(new Comparator<MyFile>() {
            @Override
            public int compare(MyFile o1, MyFile o2) {

                return o1.getFile_size().compareTo(o2.getFile_size());
            }
        });
        return returnPage(myFileList);
    }

    public String sortByFileSizeDasc() {

        myFileList.sort(new Comparator<MyFile>() {
            @Override
            public int compare(MyFile o1, MyFile o2) {

                return o2.getFile_size().compareTo(o1.getFile_size());
            }
        });
        return returnPage(myFileList);
    }

    private String returnPage(List<MyFile> myFileList) {

        session.setAttribute("myFileList", myFileList);
        session.setAttribute("page",page);

        return SUCCESS;
    }
}
